package com.itbaizhan.shopping_goods_service.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itbaizhan.shopping_common.pojo.Specification;
import com.itbaizhan.shopping_common.pojo.SpecificationOption;
import com.itbaizhan.shopping_common.pojo.SpecificationOptions;
import com.itbaizhan.shopping_common.service.SpecificationService;
import com.itbaizhan.shopping_goods_service.mapper.SpecificationMapper;
import com.itbaizhan.shopping_goods_service.mapper.SpecificationOptionMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
 * 商品规格服务实现类
 */
@DubboService
@Transactional
@Service
public class SpecificationServiceImpl implements SpecificationService {
	
	@Autowired
	private SpecificationMapper specificationMapper;
	
	@Autowired
	private SpecificationOptionMapper specificationOptionMapper;
	
	/**
	 * 添加商品规格
	 *
	 * @param specification
	 */
	public void add(Specification specification) {
		specificationMapper.insert(specification);
	}
	
	/**
	 * 修改商品规格
	 *
	 * @param specification
	 */
	public void update(Specification specification) {
		specificationMapper.updateById(specification);
	}
	
	/**
	 * 根据id查询商品规格
	 *
	 * @param id
	 * @return
	 */
	public Specification findById(Long id) {
		return specificationMapper.findById(id);
	}
	
	/**
	 * 批量删除商品规格
	 *
	 * @param ids
	 */
	public void delete(Long[] ids) {
		
		for (Long id : ids) {
			// 遍历id删除商品规格项
			QueryWrapper<SpecificationOption> wrapper = new QueryWrapper<>();
			wrapper.eq("specId", id);
			specificationOptionMapper.delete(wrapper);
			
			//删除商品规格
			specificationMapper.deleteById(id);
		}
		
		
		
	}
	
	/**
	 * 分页查询
	 *
	 * @param page
	 * @param size
	 * @return
	 */
	public Page<Specification> search(int page, int size) {
		
		return specificationMapper.selectPage(new Page<>(page, size), null);
	}
	
	/**
	 * 根据商品类型
	 *
	 * @param id
	 * @return
	 */
	public List<Specification> findByProductTypeId(Long id) {
		return specificationMapper.findByProductTypeId(id);
	}
	
	/**
	 * 批量删除规格选项
	 *
	 * @param ids
	 */
	public void deleteOption(Long[] ids) {
		specificationOptionMapper.deleteBatchIds(Arrays.asList(ids));
	}
	
	/**
	 * 添加规格选项
	 *
	 * @param specificationOptions
	 */
	public void addOption(SpecificationOptions specificationOptions) {
		//拿到规格项id
		Long specId = specificationOptions.getSpecId();
		//拿到规格项数组名
		String[] optionNames = specificationOptions.getOptionName();
		
		for (String optionName : optionNames) {
			//构建规格项对象
			SpecificationOption specificationOption = new SpecificationOption();
			specificationOption.setSpecId(specId);
			specificationOption.setOptionName(optionName);
			//存入数据库
			specificationOptionMapper.insert(specificationOption);
		}
		
		
	}
}
